package com.lzz.bottomencapsulation.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;



/**
 * Created by apple on 17/3/21.
 */
public abstract class BasePopupWindow extends PopupWindow {
    protected View.OnClickListener onClickListener;
    protected View contentView;
    protected Activity context;
    protected LayoutInflater inflater;

    public BasePopupWindow(Activity context) {
        super(context);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//防止遮挡虚拟键盘
        this.context = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(getView(), null);
        if (null != contentView) {
            setContentView(contentView);
        }
        setFocusable(true);//设置可以获取集点
        setOutsideTouchable(true);//设置点击外边可以消失
    }

    public void Show(int Gravity,View parentView) {
        if (!isShowing()) {
            showAtLocation(parentView, Gravity, 0, 0);
        }
    }

    public BasePopupWindow setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        return this;
    }

    public void setWindowbg(float f) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = f;
        context.getWindow().setAttributes(lp);
    }

    protected abstract int getView();//设置视图
}
